package org.september.pisces.filestore.service;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import org.apache.commons.io.FileUtils;
import org.september.core.component.log.LogHelper;
import org.september.core.exception.BusinessException;
import org.september.pisces.filestore.api.FileStoreService;
import org.september.pisces.filestore.entity.FileStoreItem;
import org.september.pisces.filestore.entity.FileStorePool;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class DiskFileStoreService implements FileStoreService {

    protected LogHelper log = LogHelper.getLogger(this.getClass());

    public static final String Path_Prefix = File.separator + "file" + File.separator;

    @Override
    public String saveWithThumb(FileStorePool pool, byte[] data, String ext, boolean needThumb, float thumbRate) {
        SimpleDateFormat yyyy_MM_dd_HH = new SimpleDateFormat("yyyy-MM-dd-HH");
        String path = yyyy_MM_dd_HH.format(new Date());
        String[] arr = path.split("-");
        if (!ext.startsWith(".")) {
            ext = "." + ext;
        }
        String relativePath = String.join(File.separator, arr) + File.separator + UUID.randomUUID() + ext;
        String filename = pool.getDiskPath() + File.separator + relativePath;
        try {
            File file = new File(filename);
            file.getParentFile().mkdirs();
            FileUtils.writeByteArrayToFile(file, data);
            if (needThumb) {
                File thumbnail = FileUtil.touch(file.getAbsolutePath() + ".thumb" + ext);
                ImgUtil.scale(file, thumbnail, thumbRate);//缩放比例
            }
            return Path_Prefix + pool.getPoolName() + File.separator + relativePath;
        } catch (Exception e) {
            throw new BusinessException("保存文件失败", e);
        }
    }

    @Override
    public String save(FileStorePool pool, byte[] data, String ext) {
        return saveWithThumb(pool, data, ext, false, 0);
    }

    @Override
    public String getHttpPath(String uuid) {
        return null;
    }

    @Override
    public InputStream getInputStream(FileStorePool pool, FileStoreItem item, boolean thumb) {
        String path = item.getPath().replaceFirst(File.separator + Path_Prefix + File.separator + pool.getPoolName(), "");
        String filePath = pool.getDiskPath() + path;
        if (thumb) {
            filePath = filePath + ".thumb" + item.getExt();
        }
        try {
            return FileUtils.openInputStream(new File(filePath));
        } catch (IOException e) {
            throw new BusinessException("读取文件失败", e);
        }
    }

    @Override
    public boolean deleteFile(FileStorePool pool, FileStoreItem item) {
        String path = item.getPath().replaceFirst(File.separator + Path_Prefix + File.separator + pool.getPoolName(), "");
        String filePath = pool.getDiskPath() + path;
        boolean result = FileUtils.deleteQuietly(new File(filePath));
        if (result == false) {
            log.getBuilder().warn("删除文件失败：" + filePath);
        }
        return result;
    }

}
